package com.huichaoliu.adapters;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huichaoliu.adapters.ProductAdapter.ViewHolder;
import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.rederxu.AsyncTask.ImageTask;

public class BagAdapter extends BaseAdapter{

	private Context mContext = null;
	private JSONArray ja = null;
	
	public BagAdapter(Context mContext, JSONArray jaData) {
		this.mContext = mContext;
		this.ja = jaData;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return ja.length();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		try {
			return ja.getJSONObject(position).getJSONObject("commodity").getString("image");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public String getShoppingCarId(int position){
		try {
			return ja.getJSONObject(position).getString("shoppingcaiId");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_bag, null);
			holder = new ViewHolder();
			holder.init(convertView);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.ic_radio.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!Config.isOnline(mContext)){
					return;
				}
				if(v.getTag() == null){
					v.setTag("");
					((ImageView)v).setImageResource(R.drawable.ic_round_orange);
					try {
						ja.getJSONObject(position).put("isSelected", true);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					v.setTag(null);
					((ImageView)v).setImageResource(R.drawable.ic_round_grey);
					try {
						ja.getJSONObject(position).put("isSelected", false);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		try{
			JSONObject jo = ja.getJSONObject(position);
			String cover = jo.getJSONObject("commodity").getString("image");
			String content = jo.getJSONObject("commodity").getString("name") + "——" + jo.getJSONObject("number").getString("color") + "  " + jo.getJSONObject("number").getString("size");
			String price = jo.getJSONObject("commodity").getString("price");
			ImageTask task = new ImageTask(holder.ic_avatar,Config.getFolderImages(),null);
			task.execute(cover);
			holder.tv_content.setText(content);
			holder.tv_price.setText("￥" + price);
			String type_str = "衣服";
			int type = jo.getJSONObject("commodity").getInt("type");
			if(type == 0){
				type_str = "套装";
			}else if(type == 1){
				type_str = "上衣";
			}else if(type == 2){
				type_str = "裤子";
			}else if(type == 3){
				type_str = "鞋子";
			}
			holder.tv_type.setText("｛  " + type_str + "  ｝");
		}catch(JSONException e){
			e.printStackTrace();
		}
		return convertView;
	}
	
	static class ViewHolder{
		private ImageView ic_radio;
		private ImageView ic_avatar;
		private TextView tv_content;
		private TextView tv_price;
		private TextView tv_type;
		
		private void init(View view){
			ic_radio = (ImageView)view.findViewById(R.id.ic_radio);
			ic_avatar = (ImageView)view.findViewById(R.id.ic_avatar);
			tv_content = (TextView)view.findViewById(R.id.tv_content);
			tv_price = (TextView)view.findViewById(R.id.tv_price);
			tv_type = (TextView)view.findViewById(R.id.tv_type);
		}
	}
}
